<%@page import="service.TgoodsService"%>
<%@page import="entities.TgoodsEntity"%>
<%@page import="entities.ClassEntity"%>
<%@page import="service.ClassService"%>
<%@page import="util.JsonTools"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="service.GoodsService"%>
<%@page import="entities.GoodsEntity"%>
<%@page import="service.SellerService"%>
<%@page import="entities.SellerEntity"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
	+ request.getServerName() + ":" + request.getServerPort()
	+ path + "/";
	String sellerName = (String) session.getAttribute("sellerName");
	String password = (String) session.getAttribute("sellerpassword");
	SellerEntity seller = new SellerEntity();
	if (sellerName == null || password == null) {
		response.sendRedirect("index.jsp");
	} else {
		seller = SellerService.confirm(sellerName, password.trim());
	}
	String action = request.getParameter("action");
%>

<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">

<title>菜单管理</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<script type="text/javascript" src="<%=basePath%>js/jquery.min.js"></script>
<script type="text/javascript"
	src="<%=basePath%>/_seller/js/goodsEdit.js"></script>
<link rel="stylesheet" type="text/css"
	href="<%=basePath%>user/css/user.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/cart.css">
<style type="text/css">
.action {
	margin-top: 10px;
}

.action a {
	background-color: #7CDAEF;
	padding: 5px;
	margin: 5px;
}

.action a:hover {
	background-color: #15ADFF;
	color: white;
}
</style>
</head>

<body>
	<jsp:include page="header.jsp"></jsp:include>

	<div style="display: none;" id="sellerId"><%=seller.getId()%></div>
	<div style="display: none;" id="action"><%=action%></div>

	<div style="height: 30px;"></div>
	<div class="center">
		<div class="leftPanel">
			<div class="userInfo" id="UserInfo">
				<ul>
					<li><a title="设置商标" href="_seller/sellerInfo.jsp?action=logo"><img
							src="<%=seller.getLogoURL()%>" alt="logo"> </a>
						<h3>
							[<%=seller.getName()%>]
						</h3>店主：<%=seller.getOwner()%> <br />
					<li><a class="name">手机号码：</a><a class="value"><%=seller.getTel()%></a>
					</li>
					<li><a class="name">地址：</a><a class="value"><%=seller.getAddress()%></a>
					</li>
					<li><a class="name">营业时间：</a><a class="value"><%=seller.getDeliverTime()%></a>
					</li>
					<li><a class="name">注册时间：</a><a class="value"><%=seller.getRegTime()%></a>
					</li>
				</ul>
			</div>

			<div class="thirdNav">
				<ul>
					<li id="online"><a href="_seller/menuManage.jsp?action=online">销售中的菜肴</a>
					</li>
					<%
						if ("edit".equals(action)) {
					%>
					<li id="edit"><a>编辑菜肴</a></li>
					<%
						}
					%>
					<li id="offline"><a
						href="_seller/menuManage.jsp?action=offline">已下架的菜肴</a>
					</li>
					<li id="add"><a href="_seller/menuManage.jsp?action=add&type=goods">添加菜肴</a>
					</li>
					<li id="tuangou"><a
						href="_seller/menuManage.jsp?action=tuangou">团购菜单管理</a>
					</li>

				</ul>
			</div>
		</div>
		<div class="rightPanel">
			<%
				if ("online".equals(action)) {
					List<GoodsEntity> goodsList = GoodsService
							.getSellerGoods(seller.getId() + "");
			%>
			<div class="panelTitle">
				<a>销售中的菜肴</a>
			</div>
			<div class="cart_other_products">
				<div class="mall_box" id="featured_star" style="display: block;">
					<ul class="product_list" id="product_list_star">
						<%
							for (GoodsEntity goods : goodsList) {
								SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
								SimpleDateFormat sdf2 = new SimpleDateFormat("yy年MM-dd");
						%>
						<li class="cross_sale_deal">
							<div class="pic">
								<a href="goodsDetail.jsp?goodsId=<%=goods.getId()%>"
									target="_blank"><img src="<%=goods.getImageUrl()%>"
									width="160" height="160"> </a>
							</div>
							<div class="name">
								<a href="goodsDetail.jsp?goodsId=<%=goods.getId()%>"
									target="_blank"><%=goods.getName()%></a>
							</div>
							<div class="price">
								<span>¥<%=goods.getPrice()%></span><span class="discount">&nbsp;&nbsp;上架(<%=sdf2.format(sdf1.parse(goods.getOnSaleTime()))%>)</span>
							</div>
							<div align="center" class="action">
								<a class="edit"
									href="<%=basePath%>_seller/menuManage.jsp?action=edit&goodsId=<%=goods.getId()%>">编&nbsp;&nbsp;辑</a><a
									class="offline" href="javascript:void(0);" goodsId="<%=goods.getId()%>" >下&nbsp;&nbsp;架</a>
							</div>
						</li>

						<%
							}
						%>
					</ul>
				</div>
			</div>
			<%
				} else if ("offline".equals(action)) {
						List<GoodsEntity> offlineGoods = GoodsService
								.getOfflineGoods(seller.getId()+"");
			%>
			<div class="panelTitle">
				<a>已下架的菜肴</a>
			</div>
			<div class="cart_other_products">
				<div class="mall_box" id="featured_star" style="display: block;">
					<ul class="product_list" id="product_list_star">
						<%
							for (GoodsEntity goods : offlineGoods) {
								SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
								SimpleDateFormat sdf2 = new SimpleDateFormat("yy年MM-dd");
						%>
						<li class="cross_sale_deal">
							<div class="pic">
								<a href="goodsDetail.jsp?goodsId=<%=goods.getId()%>"
									target="_blank"><img src="<%=goods.getImageUrl()%>"
									width="160" height="160"> </a>
							</div>
							<div class="name">
								<a href="goodsDetail.jsp?goodsId=<%=goods.getId()%>"
									target="_blank"><%=goods.getName()%></a>
							</div>
							<div class="price">
								<span>¥<%=goods.getPrice()%></span><span class="discount">&nbsp;&nbsp;下架(<%=sdf2.format(sdf1.parse(goods.getOfflineTime()))%>)</span>
							</div>
							<div align="center" class="action">
								<a href="javascript:void(0);" goodsId="<%=goods.getId()%>" class="online">上&nbsp;&nbsp;架</a>
							</div>
						</li>

						<%
							}
						%>
					</ul>
				</div>
			</div>
			<%
				} else if ("edit".equals(action)) {
						int goodsId = Integer.parseInt(request.getParameter("goodsId")
								.trim());
						GoodsEntity goods = GoodsService.getPointGoods(goodsId);
			%>
			<div class="panelTitle">
				<a>编辑菜肴</a>
			</div>
			<ul>
				<li style="display: none;"><a id="GoodsIdText"><%=goods.getId()%></a>
				</li>

				<li><a>菜肴名称</a></li>
				<li><input type="text" id="GoodsNameText"
					value="<%=goods.getName()%>" class="textBox">
				</li>
				<li><a>类别</a></li>
				<li><select name="goodsClass" id="GoodsClassText"
					class="textBox">
						<option value="<%=goods.getClassId()%>"><%=goods.getClassName()%></option>
						<%
							List<ClassEntity> classList =  ClassService.getAllClass();
										   for(ClassEntity cls : classList){
						%>
						<option value="<%=cls.getId()%>"><%=cls.getName()%></option>
						<%
							}
						%>
				</select>
				</li>

				<li><a>价格</a></li>
				<li><input type="text" id="GoodsPriceText"
					onkeyup="if(!/^[0-9\.]+$/.test(this.value)){alert('必须为数字!');this.value='<%=goods.getPrice()%>';}"
					value="<%=goods.getPrice()%>" class="textBox">
				</li>

				<li><a>折扣</a></li>
				<li><input type="text" id="GoodsDiscountText"
					onkeyup="if(!/^[0-9\.]+$/.test(this.value)){alert('必须为数字!');this.value='<%=goods.getPrice()%>';}"
					value="<%=goods.getDiscount()%>" class="textBox">
				</li>

				<li><a>图片</a></li>
				<li><input type="text" id="GoodsImgText" value="************"
					class="textBox"> <input type="button" class="uploadImg"
					onclick="openImgLoad('change','<%=goods.getImageUrl()%>','goods','<%=100+new Random().nextInt(100)%>');"
					style="padding: 5px;border: 1px solid;" value="上传图片" />
				<li><a>关键词</a></li>
				<li><input type="text" id="GoodsKeywordsText"
					value="<%=goods.getKeyWords()%>" class="textBox">
				</li>

				<li><a>上架时间</a></li>
				<li><input disabled="disabled" type="text"
					id="GoodsOnsaleTimeText" value="<%=goods.getOnSaleTime()%>"
					class="textBox">
				</li>

				<li><a>描述</a></li>
				<li><textarea id="GoodsDescText" class="textBox" rows="3"><%=goods.getDescrition()%>
				</textarea>
				</li>

				<li><a style="color: red;" id="ErrorTips"></a></li>
				<li><input id="Button1" class="button" type="button" value="保存"
					onclick="changeGoodsInfo();"></li>
			</ul>
			<%
				} else if ("add".equals(action)) {//新增商品
			%>
			<div class="panelTitle">
				<a>新增菜肴</a>
			</div>
			<%
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
				  //首先获得新增的商品的类型
				  String type = request.getParameter("type");
				  if("goods".equals(type)){//如果是一般商品
				  	String imgName = GoodsService.getNewGoodsName(seller.getId()+"");
				  	String imgPath = "images/goods/"+imgName;
			%>
			<ul>
				<li style="display: none;"><a id="GoodsIdText"></a></li>
				<li><a>菜肴名称</a></li>
				<li><input type="text" id="GoodsNameText" value=""
					class="textBox">
				</li>
				<li><a>类别</a></li>
				<li><select name="goodsClass" id="GoodsClassText"
					class="textBox">
						<%
							List<ClassEntity> classList =  ClassService.getAllClass();
										   for(ClassEntity cls : classList){
						%>
						<option value="<%=cls.getId()%>"><%=cls.getName()%></option>
						<%
							}
						%>
				</select>
				</li>

				<li><a>价格</a></li>
				<li><input type="text" id="GoodsPriceText"
					onkeyup="if(this.value!=''&&!/^[0-9\.]+$/.test(this.value)){alert('必须为数字!');this.value='5.0';}"
					value="0.0" class="textBox">
				</li>

				<li><a>折扣</a></li>
				<li><input type="text" id="GoodsDiscountText"
					onkeyup="if(this.value!=''&&!/^[0-9\.]+$/.test(this.value)){alert('必须为数字!');this.value='1.0';}"
					value="1.0" class="textBox">
				</li>

				<li><a>图片</a></li>
				<li><input type="text" id="GoodsImgText" value="" disabled="disabled"
					class="textBox"> <input type="button" class="uploadImg"
					onclick="openImgLoad('add','<%=imgPath%>','goods','<%=100+new Random().nextInt(100)%>');"
					style="padding: 5px;border: 1px solid;" value="上传图片" />
				<li><a>关键词</a></li>
				<li><input type="text" id="GoodsKeywordsText" value=""
					class="textBox">
				</li>

				<li><a>上架时间</a></li>
				<li><input disabled="disabled" type="text"
					id="GoodsOnsaleTimeText" value="<%=sdf.format(new Date()) %>" class="textBox">
				</li>

				<li><a>描述</a></li>
				<li><textarea id="GoodsDescText" class="textBox" rows="3"></textarea>
				</li>

				<li><a style="color: red;" id="ErrorTips"></a></li>
				<li><input id="Button1" class="button" type="button" value="确定添加"
					onclick="addGoods();"></li>
			</ul>
			<%
				}else if("tgoods".equals(type)){//如果是团购商品
				// 计算新增团购商品的商品名
					String imgName = TgoodsService.getNewTGoodsName(seller.getId()+"");
				  	String imgPath = "images/tgoods/"+imgName;
			%>
				<ul>
				<li style="display: none;"><a id="GoodsIdText"></a></li>
				<li><a>团购菜肴名称</a></li>
				<li><input type="text" id="GoodsNameText" value=""
					class="textBox">
				</li>
				<li><a>类别</a></li>
				<li><select name="goodsClass" id="GoodsClassText"
					class="textBox">
						<%
							List<ClassEntity> classList =  ClassService.getAllClass();
										   for(ClassEntity cls : classList){
						%>
						<option value="<%=cls.getId()%>"><%=cls.getName()%></option>
						<%
							}
						%>
				</select>
				</li>

				<li><a>现价</a></li>
				<li><input type="text" id="GoodsPriceText"
					onkeyup="if(this.value!=''&&!/^[0-9\.]+$/.test(this.value)){alert('必须为数字!');this.value='0.0';}"
					value="0.0" class="textBox">
				</li>

				<li><a>折扣</a></li>
				<li><input type="text" id="GoodsDiscountText"
					onkeyup="if(this.value!=''&&!/^[0-9\.]+$/.test(this.value)){alert('必须为数字!');this.value='1.0';}"
					value="1.0" class="textBox">
				</li>

				<li><a>图片</a></li>
				<li><input type="text" id="GoodsImgText" value="" disabled="disabled"
					class="textBox"> <input type="button" class="uploadImg"
					onclick="openImgLoad('add','<%=imgPath%>','tgoods','<%=100+new Random().nextInt(100)%>');"
					style="padding: 5px;border: 1px solid;" value="上传图片" />
				<li><a>关键词</a></li>
				<li><input type="text" id="GoodsKeywordsText" value=""
					class="textBox">
				</li>

				<li><a>上架时间</a></li>
				<li><input disabled="disabled" type="text"
					id="GoodsOnsaleTimeText" value="<%= sdf.format(new Date())%>" class="textBox">
				</li>

				<li><a>描述</a></li>
				<li><textarea id="GoodsDescText" class="textBox" rows="3"></textarea>
				</li>

				<li><a style="color: red;" id="ErrorTips"></a></li>
				<li><input id="Button1" class="button" type="button" value="确定添加团购菜肴"
					onclick="addTgoods();"></li>
			</ul>
			<%
				}
			%>
			<%
				} else if ("tuangou".equals(action)) {
				 	List<TgoodsEntity> tgoodsList = TgoodsService.getSellerTgoods(seller.getId()+"");
			%>
			<div class="panelTitle">
				<a>团购菜单管理</a>
				<div style=" margin-top:30px; width: 100%; height: 35px;">
					<a href="_seller/menuManage.jsp?action=add&type=tgoods">新建团购菜肴</a>
				</div>
			</div>
			<div class="cart_other_products">
				<div class="mall_box" id="featured_star" style="display: block;">
					<ul class="product_list" id="product_list_star">
						<%
							for (TgoodsEntity goods : tgoodsList) {
								SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
								SimpleDateFormat sdf2 = new SimpleDateFormat("yy年MM-dd");
						%>
						<li class="cross_sale_deal">
							<div class="pic">
								<a href="tgdetail.jsp?tgoodsId=<%=goods.getTgId()%>"
									target="_blank"><img src="<%=goods.getTgImage()%>"
									width="160" height="160"> </a>
							</div>
							<div class="name">
								<a href="tgdetail.jsp?tgoodsId=<%=goods.getTgId()%>"
									target="_blank"><%=goods.getTgName()%></a>
							</div>
							<div class="price">
								<span>¥<%=goods.getTgPrice()%></span><span class="discount">&nbsp;&nbsp;上架(<%=sdf2.format(sdf1.parse(goods.getTgSaTim()))%>)</span>
							</div>
							<div align="center" class="action">
								<a href="javascript:void(0);" goodsId="<%=goods.getTgId()%>" class="offlineTgoods">下&nbsp;&nbsp;架</a>
							</div>
						</li>
						<%
							}
						%>
					</ul>
				</div>
			</div>
			<%
				}
			%>
		</div>
	</div>
	<script type="text/javascript">
		function openImgLoad(action,imagePath,type,random)
		{
			if("add"==action){
				$(".uploadImg").attr("imagePath",imagePath+"_"+random);
			}
			window.open("<%=basePath%>_seller/imageEditor.jsp?action="
									+ action + "&size=&imagePath=" + imagePath
									+ "&type=" + type + "&random=" + random,
							"图片上传",
							"height=580,width=660,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");
		}
	</script>
</body>
</html>
